<template>
  <div class="app-container">
    <el-form :inline="true" class="demo-form-inline">
      <el-form-item>
        <el-select
          v-model="searchObj.provinceCode"
          placeholder="请选择省"
          @change="provinceChanged">
          <el-option
            v-for="item in provinceList"
            :key="item.id"
            :label="item.name"
            :value="item.id"/>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-select
          v-model="searchObj.cityCode"
          placeholder="请选择市"
          @change="cityChanged">
          <el-option
            v-for="item in cityList"
            :key="item.id"
            :label="item.name"
            :value="item.id"/>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-input v-model="searchObj.hosname" placeholder="医院名称"/>
      </el-form-item>
      <el-button type="primary" icon="el-icon-search" @click="getList()">查询</el-button>
      <el-button type="default" @click="resetData()">清空</el-button>
    </el-form>
    <!-- banner列表 -->
    <el-table v-loading="listLoading" :data="list"
              border
              fit
              highlight-current-row>
      <el-table-column
        label="序号"
        width="60"
        align="center">
        <template slot-scope="scope">
          {{ (page - 1) * limit + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column label="医院logo">
        <template slot-scope="scope">
          <img :src="'data:image/jpeg;base64,'+scope.row.logoData" width="80">
        </template>
      </el-table-column>
      <el-table-column prop="hosname" label="医院名称"/>
      <el-table-column prop="param.hosTypeString" label="等级" width="90"/>
      <el-table-column prop="param.fullAddress" label="详情地址"/>
      <el-table-column label="状态" width="80">
        <template slot-scope="scope">
          {{ scope.row.status === 0 ? '未上线' : '已上线' }}
        </template>
      </el-table-column>
      <el-table-column prop="createTime" label="创建时间"/>
      <el-table-column label="操作" width="230" align="center">
        <template slot-scope="scope">
          <router-link :to="'/hosp/hospital/schedule/'+scope.row.hoscode">
            <el-button type="primary" size="mini">排班</el-button>
          </router-link>
          <el-button v-if="scope.row.status === 1" type="danger" size="mini" @click="updateStatus(scope.row.id, 0)">
            下线
          </el-button>
          <el-button v-if="scope.row.status === 0" type="primary" size="mini" @click="updateStatus(scope.row.id, 1)">
            上线
          </el-button>
          <router-link :to="'/hosp/hospital/show/'+scope.row.hoscode">
            <el-button type="primary" size="mini">详情</el-button>
          </router-link>
        </template>
      </el-table-column>

    </el-table>
    <!-- 分页组件 -->
    <el-pagination
      :current-page="page"
      :total="total"
      :page-size="limit"
      :page-sizes="[5, 10, 20, 30, 40, 50, 100]"
      style="padding: 30px 0; text-align: center;"
      layout="sizes, prev, pager, next, jumper, ->, total, slot"
      @current-change="getList"
      @size-change="changeSize"
    />
  </div>
</template>

<script>

import hospitalApi from "@/api/hosp/hospital";
import dictApi from "@/api/cmn/dict";

export default {
  data() {
    return {
      searchObj: {}, // 搜索条件
      provinceList: [], // 省份列表
      cityList: [], // 市列表
      list: [], // 医院数据列表
      listLoading: true, // 数据加载图标
      page: 1, // 页码
      total: 0, // 总记录数
      limit: 10 // 每页条数
    }
  },
  // 初始化加载方法
  created() {
    this.getList(1)
    this.getProvinceList()
  },
  methods: {
    // 获取医院列表
    getList(page = 1) {
      this.page = page
      hospitalApi.getHospitalList(this.page, this.limit, this.searchObj).then(result => {
        this.list = result.data.content // 数据
        this.total = result.data.totalElements // 总页数
        this.listLoading = false
      })
    },
    // 获取省份列表
    getProvinceList() {
      dictApi.findByDictCode('Province').then(result => {
        this.provinceList = result.data
      })
    },

    // 省份发生变化执行该方法
    provinceChanged(id) {
      dictApi.findChildData(id).then(result => {
        this.cityList = result.data
      })
    },
    // 市发生变化执行该方法
    cityChanged() {

    },
    // 清空搜索条件
    resetData() {
      this.searchObj = {}
      this.getList(1)
    },
    // 切换每页条数执行该方法
    changeSize(size) {
      this.limit = size
      this.getList(1)
    },
    // 设置医院状态
    updateStatus(id, status) {
      hospitalApi.updateStatus(id, status).then(response => {
        this.getList(this.page)
      })
    }
  }
}

</script>
